package org.jsbd.boss.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jsbd.boss.dao.IBaseInfoDAO;
import org.jsbd.boss.dao.IMiguUserMonitorDAO;
import org.jsbd.boss.domian.statistics.MiguUserExecuteDTO;
import org.jsbd.boss.domian.statistics.MiguUserMonitorVO;
import org.jsbd.boss.domian.statistics.MiguUserStatisticsDTO;
import org.jsbd.boss.domian.statistics.ResidueStatistics;
import org.jsbd.boss.domian.statistics.UseStatisticsDto;
import org.jsbd.boss.service.IMiguUserMonitorService;
import org.jsbd.boss.util.BaseinfoUtils;
import org.jsbd.boss.util.BigDecimalUtil;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.common.utils.StringUtils;

/**
 * @author <a href="mailto:wangjunbiao@zhexinit.com" >王俊标</a>
 * @version 1.0.0
 */
@Service("miguUserMonitorService")
@SuppressWarnings("all")
public class MiguUserMonitorServiceImpl implements IMiguUserMonitorService {

	@Resource
	private IMiguUserMonitorDAO miguUserMonitorDAO;
	@Resource
	private IBaseInfoDAO baseInfoDAO;

	@Override
	public Map<String, Object> findMiguUserStatisticsInfo(MiguUserMonitorVO mumVO) {
		// 默认查询当前日期前30天的数据
		String startTime = mumVO.getStartTime();
		String endTime = mumVO.getEndTime();
		if ("".equals(startTime) && "".equals(endTime)) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			c.add(Calendar.MONTH, -1);
			String month = format.format(c.getTime());
			mumVO.setStartTime(month);
		}

		List<MiguUserStatisticsDTO> miguUserMonitorDtos = miguUserMonitorDAO.findMiguUserStatisticsInfo(mumVO);
		// 拼接前台charts显示需要的数据
		List returnList = new LinkedList();
		Map<String, Integer> residueMap = new HashMap<String, Integer>();

		List chartText = new ArrayList();
		List chartDays = new ArrayList();
		chartText.add("days");
		for (MiguUserStatisticsDTO musDTO : miguUserMonitorDtos) {
			if (!chartText.contains(musDTO.getBaseId())) {
				chartText.add(musDTO.getBaseId());
			}
			if (!chartDays.contains(musDTO.getDays())) {
				chartDays.add(musDTO.getDays());
			}
		}

		for (int i = 0; i < chartDays.size(); i++) {
			String days = (String) chartDays.get(i);
			List chartValue = new ArrayList();
			chartValue.add(chartDays.get(i));

			for (int j = 1; j < chartText.size(); j++) {
				int chartTextBaseId = (int) chartText.get(j);
				for (MiguUserStatisticsDTO musDTO : miguUserMonitorDtos) {
					String musDays = musDTO.getDays();
					if (!musDays.equals(days)) {
						continue;
					}

					int baseId = musDTO.getBaseId();
					if (chartTextBaseId == baseId) {
						chartValue.add(musDTO.getCount());
					}

					residueMap.put(BaseinfoUtils.getBaseName(baseId), musDTO.getResidue());
				}
			}
			if (chartValue.size() < chartText.size()) {
				int diff = chartText.size() - chartValue.size();
				for (int k = 0; k < diff; k++) {
					chartValue.add(0);
				}
			}
			returnList.add(chartValue);
		}

		for (int j = 1; j < chartText.size(); j++) {
			String baseName = BaseinfoUtils.getBaseName((int) chartText.get(j));
			chartText.set(j, baseName);
		}
		returnList.add(0, chartText);

		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("residue", residueMap);
		returnMap.put("charts", returnList);

		return returnMap;
	}

	@Override
	public Map<String, Object> findMiguUserDayStatisticsInfo(MiguUserMonitorVO mumVO) {
		Map<String, Object> map = new HashMap<String, Object>();

		List<String> xAxis = new ArrayList<String>();
		List<MiguUserExecuteDTO> yAxis1 = miguUserMonitorDAO.selectExecuteYAxis1(mumVO);
		List<MiguUserExecuteDTO> yAxis2 = miguUserMonitorDAO.selectExecuteYAxis2(mumVO);
		List<MiguUserExecuteDTO> yAxis3 = miguUserMonitorDAO.selectExecuteYAxis3(mumVO);
		for (int i = 0; i < yAxis1.size(); i++) {
			xAxis.add(yAxis1.get(i).getxAxis());
			yAxis1.get(i).setRead(yAxis2.get(i).getRead());
			yAxis1.get(i).setLingxi(yAxis3.get(i).getLingxi());
		}

		map.put("xAxis", xAxis);
		map.put("yAxis", yAxis1);
		return map;

		// 默认查询2小时内数据
		/*
		String startTime = mumVO.getStartTime();
		String endTime = mumVO.getEndTime();
		if ("".equals(startTime) && "".equals(endTime)) {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar c = Calendar.getInstance();
			c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) - 2);
			mumVO.setStartTime(format.format(c.getTime()));
		}

		List returnList = new LinkedList();

		List<String> bases = miguUserMonitorDAO.findMiguUserDayBaseInfo(mumVO);
		bases.add(0, "time");
		List<MiguUserStatisticsDTO> miguUserMonitorDtos = miguUserMonitorDAO.findMiguUserDayStatisticsInfo(mumVO);
		List<String> time = miguUserMonitorDAO.findMiguUserDayTimeInfo(mumVO);
		for (String t : time) {
			List chartValue = new ArrayList();
			chartValue.add(t.substring(11, 19));
			for (int i = 1; i < bases.size(); i++) {
				for (MiguUserStatisticsDTO musDto : miguUserMonitorDtos) {
					String createTime = musDto.getCreateTime();
					if (!createTime.equals(t)) {
						continue;
					}

					int baseId = musDto.getBaseId();
					if (Integer.valueOf(bases.get(i)) == baseId) {
						chartValue.add(musDto.getCount());
					}
				}
			}

			if (chartValue.size() < bases.size()) {
				int diff = bases.size() - chartValue.size();
				for (int k = 0; k < diff; k++) {
					chartValue.add(null);
				}
			}
			returnList.add(chartValue);
		}

		for (int j = 1; j < bases.size(); j++) {
			String baseName = BaseinfoUtils.getBaseName(Integer.valueOf(bases.get(j)));
			bases.set(j, baseName);
		}
		returnList.add(0, bases);
		return returnList;
		*/
	}
	
	@Override
	public List<String> selectDays(MiguUserMonitorVO mumVO) {
		return miguUserMonitorDAO.selectDays(mumVO);
	}

	@Override
	public List<MiguUserStatisticsDTO> selectRecords(MiguUserMonitorVO mumVO) {
		mumVO.setBase("0");
		List<Integer> list_music = miguUserMonitorDAO.selectRecords(mumVO);
		mumVO.setBase("3");
		List<Integer> list_read = miguUserMonitorDAO.selectRecords(mumVO);
		mumVO.setBase("6");
		List<Integer> list_lingxi = miguUserMonitorDAO.selectRecords(mumVO);
		
		List<MiguUserStatisticsDTO> dtos = new ArrayList<MiguUserStatisticsDTO>();
		for (int i = 0; i < list_music.size(); i++) {
			MiguUserStatisticsDTO temp = new MiguUserStatisticsDTO();
			temp.setMusic(list_music.get(i));
			temp.setRead(list_read.get(i));
			temp.setLingxi(list_lingxi.get(i));
			dtos.add(temp);
		}
		return dtos;
	}

	@Override
	public List<MiguUserStatisticsDTO> selectResidue(String date) {
		return miguUserMonitorDAO.selectResidue(date);
	}

}
